package com.srit.recipes.service.impl;

import com.srit.recipes.common.parent.DaoInject;
import com.srit.recipes.common.response.JpaPage;
import com.srit.recipes.entity.CodeInfo;
import com.srit.recipes.entity.IngredientInfo;
import com.srit.recipes.entity.RecipesInfo;
import com.srit.recipes.service.BaseService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BaseServiceImpl extends DaoInject implements BaseService {


    @Override
    public List<CodeInfo> codeInfo() {
        return codeInfoDao.findAll();
    }

    @Override
    public Object recipesInfo(JpaPage page, RecipesInfo recipesInfo) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("cpName", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<RecipesInfo> example = Example.of(recipesInfo, exampleMatcher);


        return JpaPage.build(recipesDao.findAll(example, JpaPage.getPageable(page)));
    }

    @Override
    public JpaPage scyy(JpaPage page, String name) {
        if (StringUtils.hasLength(name)) {
            return JpaPage.build(scyyInfoDao.findAllBySCNAMEContains(name, JpaPage.getPageable(page)));
        } else {
            return JpaPage.build(scyyInfoDao.findAll(JpaPage.getPageable(page)));
        }
    }

    @Override
    public List<IngredientInfo> recipesIngredient(String recipesId) {

        return ingredientDao.findAllByCpId(recipesId);
    }

    @Override
    public Object recipesNutrition(JpaPage page, String name) {
        if (StringUtils.hasLength(name)) {
            return JpaPage.build(recipesNutritionDao.findAllByCpNameContains(name, JpaPage.getPageable(page)));
        } else {
            return JpaPage.build(recipesNutritionDao.findAll(JpaPage.getPageable(page)));
        }
    }

    @Override
    public Object nutritionStandard(JpaPage page, int type) {
        return JpaPage.build(nutritionStandardDao.findAllByType(type, JpaPage.getPageable(page)));
    }
}
